clear;clc;
%%
tc=EquipmentControl.AuxiliaryEquipment.TemperatureController.TemperatureController('COM8');
tc.connect_device;
app=tc.gui();

%%
temperature1 = 90; duration1 = 600; temperature2 = 110; duration2 = 600;
pVal = 4; tauIList = 800:200:1600; tauD = 0; 

k = 1; results = cell(size(tauIList));
for tauI = tauIList
    stepResponse = tc.DO_StepResponse(app, pVal, tauI, tauD, temperature1, duration1, temperature2, duration2);
    plot_data(stepResponse, pVal, tauI, tauD)
    results{k} = stepResponse;
    k=k+1;
end

%%
save('TemperatureStepResponse_P4.mat', 'results');
%%
function plot_data(measured_data, pVal, tauI, tauD)
    tmList = measured_data.tList;
    tpList = 0.1*(measured_data.data(1,:)+measured_data.data(2, :)*256);
    targetList = 0.1*(measured_data.data(3,:)+measured_data.data(4, :)*256);
    vList = measured_data.data(5, :)*5.0/100;
    figure('Name', sprintf('P=%3.1f, tauI=%d s, tauD=%3.1f s', pVal, tauI, tauD)); 
    ax1 = subplot(2, 1, 1);
    plot(ax1, tmList, tpList, 'bo-', tmList, targetList, 'b--');grid(ax1, 'on'); xlim(ax1, minmax(tmList)); ylabel(ax1, 'Temperature(\circC)')
    ax2 = subplot(2, 1, 2);
    plot(ax2, tmList, vList, 'rd-');grid(ax2, 'on'); xlim(ax2, minmax(tmList)); ylabel(ax2, 'Control Voltage (V)'); xlabel(ax2, 'Time (s)');
end